ThongKePanel = Ext.extend(AbstractPanel, {
	layout: 'anchor',
    defaults: {anchor: '100%'},
    
    initComponent : function() {
    	this.formLabel = new Ext.form.Label({
			cls : 'main-title',
			text : lang.text.menu.thongKeHd.toUpperCase()
		});
		
		this.formLabelContainer = new Ext.Container({
			cls : 'field-align-center',
			items : [this.formLabel]
		});
		
		var hdButtonBar = this.initThongKeHdButtonsBar();
		this.thongKeTable = this.initThongKeTable();
		var gridCtn = new Ext.Container({
			layout : 'customizedlayout',
			items : [this.thongKeTable]
		});
		this.thongKeHdFieldSet = new Ext.form.FieldSet({
			title : lang.text.menu.thongKeHd,
			autoHeight : true,
			height : 500,
			layout : 'customizedlayout',
			items : [hdButtonBar, {xtype : 'spacer', height : 20}, gridCtn]
		});
		
		var ptButtonBar = this.initThongKePtButtonsBar();
		var dateCtn = this.initDateCtn();
		this.thongKePtFieldSet = new Ext.form.FieldSet({
			title : lang.text.menu.thongKePt,
			autoHeight : true,
			height : 100,
			layout : 'customizedlayout',
			items : [dateCtn, {xtype : 'spacer', height : 10}, ptButtonBar]
		});
		
    	this.items = [this.formLabelContainer, 
    	              {xtype : 'spacer', height : 10}, this.thongKeHdFieldSet, 
    	              {xtype : 'spacer', height : 10}, this.thongKePtFieldSet];
    	ThongKePanel.superclass.initComponent.call(this);
    },
    
    initThongKeHdButtonsBar : function() {
    	this.taoThongKeHdBtn = new Ext.Button({
			width : 100,
			text : lang.text.thongKe.taoThongKe
		});
		
		this.xuatThongKeHdBtn = new Ext.Button({
			width : 100,
			text : lang.text.thongKe.xuatThongKe
		});
		
		return new Ext.Container({
			layout : 'hbox',
			items : [this.taoThongKeHdBtn, {xtype : 'spacer', width : 13}, this.xuatThongKeHdBtn]
		});
    },
    
    initThongKeTable : function() {
    	this.gridStore = new Ext.data.JsonStore({
    		totalProperty : 'count',
    		root : 'items',
    		fields : [
                    {name : 'ngayLapText'},
                    {name : 'tongSoHd'},
                    {name : 'tongSoHdDangGop'},
                    {name : 'tongSoHdTraHang'},
                    {name : 'tongSoHdDenHang'},
                    {name : 'tongDoanhThuLyThuyet'},
                    {name : 'tongDoanhThuThucTe'},
                    {name : 'tongDaThu'},
                    {name : 'tongPhaiThu'}]
    	});
    	
    	this.gridColModel = new Ext.grid.ColumnModel({
            defaults : {
                sortable : true
            },
            columns : [ {
                id : 'ngayLapText',
                header : lang.text.thongKe.ngayTao,
                dataIndex : 'ngayLapText',
                fixed : true,
                width : 80
            }, {
                id : 'tongSoHd',
                header : lang.text.thongKe.tongHd,
                dataIndex : 'tongSoHd',
                fixed : true,
                renderer : function(val) {
                	return Utility.formatThousandSeparator(val);
                },
                width : 70
            }, {
                id : 'tongSoHdDangGop',
                header : lang.text.thongKe.soHdDangGop,
                dataIndex : 'tongSoHdDangGop',
                fixed : true,
                renderer : function(val) {
                	return Utility.formatThousandSeparator(val);
                },
                width : 100
            }, {
                id : 'tongSoHdTraHang',
                header : lang.text.thongKe.soHdTraHang,
                dataIndex : 'tongSoHdTraHang',
                fixed : true,
                renderer : function(val) {
                	return Utility.formatThousandSeparator(val);
                },
                width : 100
            }, {
                id : 'tongSoHdDenHang',
                header : lang.text.thongKe.soHdDenHang,
                dataIndex : 'tongSoHdDenHang',
                renderer : function(val) {
                	return Utility.formatThousandSeparator(val);
                },
                width : 100
            }, {
                id : 'tongDoanhThuLyThuyet',
                header : lang.text.thongKe.doanhThuLyThuyet,
                dataIndex : 'tongDoanhThuLyThuyet',
                renderer : function(val) {
                	return Utility.formatThousandSeparator(val);
                },
                width : 160
            }, {
                id : 'tongDoanhThuThucTe',
                header : lang.text.thongKe.doanhThuThucTe,
                dataIndex : 'tongDoanhThuThucTe',
                renderer : function(val) {
                	return Utility.formatThousandSeparator(val);
                },
                width : 160
            }, {
                id : 'tongDaThu',
                header : lang.text.thongKe.tongDaThu,
                dataIndex : 'tongDaThu',
                fixed : true,
                renderer : function(val) {
                	return Utility.formatThousandSeparator(val);
                },
                width : 110
            }, {
                id : 'tongPhaiThu',
                header : lang.text.thongKe.tongPhaiThu,
                dataIndex : 'tongPhaiThu',
                renderer : function(val) {
                	return Utility.formatThousandSeparator(val);
                },
                width : 110
            }]
        });
    	
    	return new Ext.grid.GridPanel({
    		store : this.gridStore,
    		height : 400,
    		viewConfig : {
    			forceFit : true
    		},
    		colModel : this.gridColModel
    	});
    },
    
    initThongKePtButtonsBar : function() {
    	this.thongKeGiaoPhieuBtn = new Ext.Button({
			width : 130,
			text : lang.text.thongKe.thongKeGiaoPhieu
		});
		
		this.thongKeGiaoPhieuDailyBtn = new Ext.Button({
			width : 150,
			text : lang.text.thongKe.thongKeGiaoPhieuDaily
		});
		
		return new Ext.Container({
			layout : 'hbox',
			items : [this.thongKeGiaoPhieuBtn, {xtype : 'spacer', width : 13}, this.thongKeGiaoPhieuDailyBtn]
		});
    },
    
    initDateCtn : function() {
    	var firstDay = new Date();
		firstDay.setDate(1);
    	this.thongKePtFromDate = new ThienTanDateField({
			fieldLabel : lang.text.pt.thongKeFrom,
			width : 90,
			value : firstDay,
			name: 'ngayBanFrom'
		});
		
		this.thongKePtToDate = new ThienTanDateField({
			fieldLabel : lang.text.pt.thongKeTo,
			width : 90,
			value : new Date(),
			name: 'ngayBanTo'
		});
		
		return new Ext.Container({
			layout : 'column',
			defaults : {
				columnWidth : .5
			},
			width : 500,
			items : [Utility.createLayoutForComponent(this.thongKePtFromDate, 110), 
			         Utility.createLayoutForComponent(this.thongKePtToDate, 15)]
		});
    },
    
    afterRender : function() {
    	this.submitButtons = [this.taoThongKeHdBtn, this.xuatThongKeHdBtn, 
    	                      this.thongKeGiaoPhieuBtn, this.thongKeGiaoPhieuDailyBtn];
    	this.taoThongKeHdBtn.on('click', this.taoThongKeHd, this);
    	this.xuatThongKeHdBtn.on('click', this.xuatThongKeHd, this);
    	this.thongKeGiaoPhieuBtn.on('click', this.xuatThongKeGiaoPhieu, this);
    	this.thongKeGiaoPhieuDailyBtn.on('click', this.xuatThongKeGiaoPhieuDaily, this);
    	this.taoThongKeHdBtn.on('afterrender', function(){
    		this.taoThongKeHdBtn.focus();	
		}, this);
    	this.taoThongKeHd(true);
    	ThongKePanel.superclass.afterRender.call(this);
    },
    
    taoThongKeHd : function(firstLoad) {
    	var waitingBox = null
    	if (typeof firstLoad == "object") { // button
    		waitingBox = Ext.MessageBox.wait(lang.text.pleaseWait, lang.text.loading);
		}
    	Ext.Ajax.request({
            url : 'thongKeHopDong/xemThongKeHd',
            params : {firstLoad : (typeof firstLoad == "boolean")}, //first load when opening this screen
            method : 'GET',
            scope : this,
            success : function(response, opts){
                var result = Ext.decode(response.responseText);
                if (result.success) {
                	this.gridStore.loadData(result.data);
				} else {
					this.notifyMessage({action : 'error', msg : result.msg})
				}
                if (waitingBox) {
					waitingBox.hide();
				}
            }, 
            failure : Ext.emptyFn
        });
    },
    
    xuatThongKeHd : function() {
    	this.disableButtons();
    	Utility.processExportWaiting("thongKeHopDong/export");
    	this.enableButtons();
    },
    
    xuatThongKeGiaoPhieu : function() {
    	var params = {
    		"fromDate" : this.thongKePtFromDate.getValue(),	
    		"toDate" : this.thongKePtToDate.getValue()
    	};
    	this.disableButtons();
    	Utility.processExportWaitingWithParams("thongKePhieuThu/export", params);
    	this.enableButtons();
    },
	
	defaultActionHandler : function(){
		this.taoThongKeHd();
	}
});